﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using BaseTerminalProtocol.Authorizations;
using Helpers;

namespace BaseTerminalProtocol.TerminalActions.Providers
{
    public class CheckPaymentRequisites
    {
        /// <summary>
        /// Requests the get agent info.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="paymentId">The payment id.</param>
        /// <param name="amountFrom">The amount from.</param>
        /// <param name="amountTo">The amount to.</param>
        /// <param name="service">The service.</param>
        /// <param name="account">The account.</param>
        /// <param name="receiptId">The receipt id.</param>
        /// <param name="receiptDate">The receipt date.</param>
        /// <returns></returns>
        public static XmlDocument RequestCheckPaymentRequisites(Authorizations.Authorization authorization, string paymentId, 
            string amountFrom, string amountTo, string service, string account,string receiptId,DateTime receiptDate)
        {
            XmlDocument doc = XMLGate.Request_INIT(authorization);
            XmlElement xmlRoot = doc.DocumentElement;

            XmlElement xmlProviders = XMLHelper.newElement(doc, "providers");
            XmlElement xmlCheckPaymentRequisites = XMLHelper.newElement(doc, "checkPaymentRequisites");
            XmlElement xmlPayment = XMLHelper.newElement(doc, "payment");
            xmlPayment.Attributes.Append(XMLHelper.newAttribute(doc, "id", paymentId));
            
            XmlElement xmlFrom = XMLHelper.newElement(doc, "from");
            xmlFrom.Attributes.Append(XMLHelper.newAttribute(doc, "currency", "643"));
            xmlFrom.Attributes.Append(XMLHelper.newAttribute(doc, "amount", amountFrom));
           
            XmlElement xmlTo = XMLHelper.newElement(doc, "to");
            xmlTo.Attributes.Append(XMLHelper.newAttribute(doc, "currency", "643"));
            xmlTo.Attributes.Append(XMLHelper.newAttribute(doc, "service", service));
            xmlFrom.Attributes.Append(XMLHelper.newAttribute(doc, "amount", amountTo));
            xmlFrom.Attributes.Append(XMLHelper.newAttribute(doc, "account", account));

            XmlElement xmlReceipt = XMLHelper.newElement(doc, "receipt");
            xmlReceipt.Attributes.Append(XMLHelper.newAttribute(doc, "id", receiptId));
            xmlReceipt.Attributes.Append(XMLHelper.newAttribute(doc, "date", receiptDate.ToString()));

            xmlPayment.AppendChild(xmlFrom);
            xmlPayment.AppendChild(xmlTo);
            xmlPayment.AppendChild(xmlReceipt);

            xmlCheckPaymentRequisites.AppendChild(xmlPayment);
            xmlProviders.AppendChild(xmlCheckPaymentRequisites);
            xmlRoot.AppendChild(xmlProviders);

            return doc;
        }

        /// <summary>
        /// Responses the get agent info.
        /// </summary>
        /// <param name="paymentId">The payment id.</param>
        /// <param name="amountFrom">The amount from.</param>
        /// <param name="amountTo">The amount to.</param>
        /// <param name="service">The service.</param>
        /// <param name="account">The account.</param>
        /// <param name="receiptId">The receipt id.</param>
        /// <param name="receiptDate">The receipt date.</param>
        /// <returns></returns>
        public static XMLGate.GateResponse ResponseCheckPaymentRequisites(string paymentId,
            string amountFrom, string amountTo, string service, string account, string receiptId, DateTime receiptDate)
        {
            try
            {
                var authorization = new Authorization();
                string request = "";
                var requestGetAgentInfo = RequestCheckPaymentRequisites(authorization, paymentId,amountFrom,amountTo,service,account,receiptId,receiptDate);
                var requestSend = XMLGate.Request_SEND(requestGetAgentInfo, authorization.ServerUrl, out request);
                var response = new XMLGate.GateResponse(requestSend);
                return response;
            }
            catch (Exception ex)
            {
                Tools.Logger("CheckPaymentRequisites : " + ex.Message, "error", "ResponseCheckPaymentRequisites");
            }
            return null;
        }
    }
}
